import * as echarts from 'echarts';

var option;
var myChart

export function init(el) {
  // 销毁当前图表实例
  myChart && myChart.dispose();
  var chartDom = document.getElementById(el);
  myChart = echarts.init(chartDom);
  myChart.showLoading({
    text: 'loading',
  });
}

export function setOption(xData, campaignData, videoData) {
  option = {
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow'
      }
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true
    },
    xAxis: [{
      type: 'category',
      data: xData,
      axisTick: {
        alignWithLabel: true
      }
    }],
    yAxis: [{
      type: 'value'
    }],
    series: [
      {
        name: '计划数量',
        type: 'bar',
        data: campaignData
      },{
        name: '视频数量',
        type: 'bar',
        data: videoData
      }
    ]
  };
  option && myChart.setOption(option);
  myChart.hideLoading()
}

export function setOption2(xData, series) {
  // 动态自定义 series
  option = {
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow'
      }
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true
    },
    xAxis: [{
      type: 'category',
      data: xData,
      axisTick: {
        alignWithLabel: true
      }
    }],
    yAxis: [{
      type: 'value'
    }],
    series,
  };
  option && myChart.setOption(option, {
    notMerge: true // 强制不合并，确保旧数据被清除  
  });
  myChart.hideLoading()
}

export function setOption3(xData, series, legend) {
  // 动态自定义 series
  option = {
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow'
      }
    },
    legend,
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true
    },
    xAxis: [{
      type: 'category',
      data: xData,
      axisTick: {
        alignWithLabel: true
      }
    }],
    yAxis: [{
      type: 'value'
    }],
    series,
  };
  console.log(option);
  option && myChart.setOption(option, {
    notMerge: true // 强制不合并，确保旧数据被清除  
  });
  myChart.hideLoading()
}